﻿using System.Collections.Generic;
using System.Data;
using DocumentFormat.OpenXml.Spreadsheet;
using eCommerce.db.DALRespository;
using eCommerce.db.DataRespository;
using eCommerce.core.Helpers;
using System.Web;
using System;
using System.Web.Caching;

namespace eCommerce.db.BORespository
{
    public class ProductBo
    {
        public static DataTable GetListByCategoryId(int categoryId)
        {
            return ProductDal.GetListByCategoryId(categoryId);
        }

        public static DataTable GetListByLstProduct(int categoryId, string lstProduct)
        {
            return ProductDal.GetListByLstProduct(categoryId, lstProduct);
        }

        public static List<ProductEntity> GetListFilter(string lstCategory, string lstPropertyValue, string orderBy, int pageIndex, int pageSize, ref int totalRecords)
        {
            List<ProductEntity> list = null;
            var cacheName = Utils.GET_PRODUCT_GETLISTFILTER(lstCategory, lstPropertyValue, orderBy, pageIndex, pageSize);

            list = Utils.GetFromCacheIIS<List<ProductEntity>>(cacheName);
            if (list == null)
            {
                list = ProductDal.GetListFilter(lstCategory, lstPropertyValue, orderBy, pageIndex, pageSize, ref totalRecords);
                HttpContext.Current.Cache.Insert(cacheName, list, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
            }
            return list;
        }

        public static List<ProductEntity> GetListLatest(int count, int status)
        {
            List<ProductEntity> list = null;
            var cacheName = Utils.GET_PRODUCT_GETLISTLATEST(count, status);

            list = Utils.GetFromCacheIIS<List<ProductEntity>>(cacheName);
            if (list == null)
            {
                list = ProductDal.GetListLatest(count, status);
                HttpContext.Current.Cache.Insert(cacheName, list, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
            }
            return list;
        }

        public static List<ProductEntity> GetListLatest_Paging(int status,int pageIndex, int pageSize, ref int totalRecords)
        {
            List<ProductEntity> list = null;
            var cacheName = Utils.GET_PRODUCT_GETLISTLATEST_PAGING(status, pageIndex, pageSize);

            list = Utils.GetFromCacheIIS<List<ProductEntity>>(cacheName);
            if (list == null)
            {
                list = ProductDal.GetListLatest_Paging(status, pageIndex, pageSize,ref totalRecords);
                HttpContext.Current.Cache.Insert(cacheName, list, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
            }
            return list;
        }

        public static List<ProductEntity> GetListDiscount(int count)
        {
            List<ProductEntity> list = null;
            var cacheName = Utils.GET_PRODUCT_GETLISTDISCOUNT(count);

            list = Utils.GetFromCacheIIS<List<ProductEntity>>(cacheName);
            if (list == null)
            {
                list = ProductDal.GetListDiscount(count);
                HttpContext.Current.Cache.Insert(cacheName, list, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
            }
            return list;
        }

        public static List<ProductEntity> GetListDiscount_Paging(int pageIndex, int pageSize, ref int totalRecords)
        {
            List<ProductEntity> list = null;
            var cacheName = Utils.GET_PRODUCT_GETLISTDISCOUNT_PAGING(pageIndex,pageSize);

            list = Utils.GetFromCacheIIS<List<ProductEntity>>(cacheName);
            if (list == null)
            {
                list= ProductDal.GetListDiscount_Paging(pageIndex, pageSize, ref totalRecords);
                HttpContext.Current.Cache.Insert(cacheName, list, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
            }
            return list;
        }

        public static List<ProductEntity> GetListProperty(int productId)
        {
            List<ProductEntity> list = null;
            var cacheName = Utils.GET_PRODUCT_GETLISTPROPERTY(productId);

            list = Utils.GetFromCacheIIS<List<ProductEntity>>(cacheName);
            if (list == null)
            {
                list = ProductDal.GetListProperty(productId);
                HttpContext.Current.Cache.Insert(cacheName, list, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
            }
            return list;
        }

        public static List<ProductEntity> GetRelateProduct(int Top,int productId, int categoryId)
        {
            return ProductDal.GetRelateProduct(Top,productId, categoryId);
        }

        public static List<ProductEntity> Search(string keyword, int categoryId, int status, int orderBy, int pageIndex, int pageSize, ref int totalRecords)
        {
            return ProductDal.Search(keyword, categoryId, status, orderBy, pageIndex, pageSize, ref totalRecords);
        }

        public static ProductEntity GetById(int productId)
        {
            return ProductDal.GetById(productId);
        }        

        public static void Save(ProductEntity o)
        {
            ProductDal.Save(o);
        }        

        public static int Create(ProductEntity o)
        {
            return ProductDal.Create(o);
        }

        public static void Delete(int id)
        {
            ProductDal.Delete(id);
        }

        public static void UpdateStatus(int id, int status)
        {
            ProductDal.UpdateStatus(id, status);
        }

        public static void UpdateImagePath(int id, string imgPath)
        {
            ProductDal.UpdateImagePath(id, imgPath);
        }

        public static int GetStatistic(int status)
        {
            return ProductDal.GetStatistic(status);
        }
        
        public static List<ProductEntity> GetDataExcel(int categoryId, int status, int propertyType, Worksheet worksheet, SharedStringTable sharedString, ref string errorMessage)
        {
            return ProductDal.GetDataExcel(categoryId, status, propertyType, worksheet, sharedString, ref errorMessage);
        }
    }
}
